package com.jmu.edu.IMApplication.utils;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.List;

import cn.jpush.im.android.api.JMessageClient;
import cn.jpush.im.android.api.callback.RequestCallback;
import cn.jpush.im.android.api.content.CustomContent;
import cn.jpush.im.android.api.content.EventNotificationContent;
import cn.jpush.im.android.api.content.FileContent;
import cn.jpush.im.android.api.content.ImageContent;
import cn.jpush.im.android.api.content.LocationContent;
import cn.jpush.im.android.api.content.MessageContent;
import cn.jpush.im.android.api.content.PromptContent;
import cn.jpush.im.android.api.content.TextContent;
import cn.jpush.im.android.api.content.VideoContent;
import cn.jpush.im.android.api.content.VoiceContent;
import cn.jpush.im.android.api.enums.ContentType;
import cn.jpush.im.android.api.exceptions.JMFileSizeExceedException;
import cn.jpush.im.android.api.model.Conversation;
import cn.jpush.im.android.api.model.Message;
import cn.jpush.im.android.api.options.MessageSendingOptions;
import cn.jpush.im.api.BasicCallback;

public class MsgUtils {

    /**
     * 创建单聊会话，如果本地已存在该会话则不会重复创建
     * @param username
     */
    public static void createSingleConversation(String username)
    {
        Conversation.createSingleConversation(username, "");
    }

    /**
     * 创建群聊会话
     * @param groupID
     */
    public static void createGroupConversation(long groupID)
    {
        Conversation.createGroupConversation(groupID);
    }

    /**
     * 获得单聊会话
     * @param username
     * @return
     */
    public static Conversation getSingleConversation(String username) {
        Conversation conversation = JMessageClient.getSingleConversation(username, "");
        if (conversation == null) {
            conversation = Conversation.createSingleConversation(username, "");
        }
        return conversation;
    }

    /**
     * 获得群聊会话
     * @param groupId
     * @return
     */
    public static Conversation getGroupConversation(long groupId) {
        Conversation conversation = JMessageClient.getGroupConversation(groupId);
        if (conversation == null) {
            conversation = Conversation.createGroupConversation(groupId);
        }
        return conversation;
    }

    /**
     * 删除单聊会话，同时删除本地聊天记录
     * @param username
     */
    public static void deleteSingleConversation(String username)
    {
        JMessageClient.deleteSingleConversation(username, "");
    }

    /**
     * 删除群聊会话
     * @param groupID
     */
    public static void deleteGroupConversation(long groupID)
    {
        JMessageClient.deleteGroupConversation(groupID);
    }

    /**
     * 获取所有会话未读消息总数
     * @return
     */
    public static int getAllUnReadMsgCount()
    {
        return JMessageClient.getAllUnReadMsgCount();
    }

    /**
     * 进入单聊会话，进入单聊界面时调用
     *
     * @param username 聊天对象用户名
     */
    public static void enterSingleConversation(String username) {
        JMessageClient.enterSingleConversation(username, "");
    }

    /**
     * 进入群聊会话，进入群聊界面时调用
     *
     * @param groupID 群聊ID
     */
    public static void enterGroupConversation(long groupID) {
        JMessageClient.enterGroupConversation(groupID);
    }

    /**
     * 退出会话，退出聊天界面时调用
     */
    public static void exitConversation() {
        JMessageClient.exitConversation();
    }

    /**
     * 创建单聊文本消息
     *
     * @param username
     * @param text
     * @param sendCompleteCallback
     * @return
     */
    public static Message createSingleTextMessage(String username, String text, BasicCallback sendCompleteCallback) {
        Message message = JMessageClient.createSingleTextMessage(username, "", text);
        message.setOnSendCompleteCallback(sendCompleteCallback);
        return message;
    }

    /**
     * 创建群组文本消息
     *
     * @param groupID
     * @param text
     * @param sendCompleteCallback
     * @return
     */
    public static Message createGroupTextMessage(long groupID, String text, BasicCallback sendCompleteCallback) {
        Message message = JMessageClient.createGroupTextMessage(groupID, text);
        message.setOnSendCompleteCallback(sendCompleteCallback);
        return message;
    }

    /**
     * 创建单聊图片消息
     *
     * @param username
     * @param imageFile
     * @param sendCompleteCallback
     * @return
     * @throws FileNotFoundException
     */
    public static Message createSingleImageMessage(String username, File imageFile, BasicCallback sendCompleteCallback)
            throws FileNotFoundException {
        Message message = JMessageClient.createSingleImageMessage(username, "", imageFile);
        message.setOnSendCompleteCallback(sendCompleteCallback);
        return message;
    }

    /**
     * 创建群聊图片消息
     *
     * @param groupID
     * @param imageFile
     * @param sendCompleteCallback
     * @return
     * @throws FileNotFoundException
     */
    public static Message createGroupImageMessage(long groupID, File imageFile, BasicCallback sendCompleteCallback)
            throws FileNotFoundException {
        Message message = JMessageClient.createGroupImageMessage(groupID, imageFile);
        message.setOnSendCompleteCallback(sendCompleteCallback);
        return message;
    }

    /**
     * 创建单聊文件消息
     *
     * @param username
     * @param file
     * @param fileName
     * @param sendCompleteCallback
     * @return
     * @throws FileNotFoundException
     * @throws JMFileSizeExceedException
     */
    public static Message createSingleFileMessage(String username, File file, String fileName, BasicCallback sendCompleteCallback)
            throws FileNotFoundException, JMFileSizeExceedException {
        Message message = JMessageClient.createSingleFileMessage(username, "", file, fileName);
        message.setOnSendCompleteCallback(sendCompleteCallback);
        return message;
    }

    /**
     * 创建群聊文件消息
     *
     * @param groupID
     * @param file
     * @param fileName
     * @param sendCompleteCallback
     * @return
     * @throws FileNotFoundException
     * @throws JMFileSizeExceedException
     */
    public static Message createGroupFileMessage(long groupID, File file, String fileName, BasicCallback sendCompleteCallback)
            throws FileNotFoundException, JMFileSizeExceedException {
        Message message = JMessageClient.createGroupFileMessage(groupID, file, fileName);
        message.setOnSendCompleteCallback(sendCompleteCallback);
        return message;
    }

    /**
     * 发送消息
     * @param message
     */
    public static void sendMessage(Message message)
    {
        MessageSendingOptions sendingOptions = new MessageSendingOptions();
        sendingOptions.setShowNotification(false);
        JMessageClient.sendMessage(message, sendingOptions);
    }

    /**
     * 发消息时支持一些可选参数的配置
     * @param message
     * @param options
     */
    public static void sendMessage(Message message, MessageSendingOptions options)
    {
        JMessageClient.sendMessage(message, options);
    }

    /**
     * 转发消息，支持转发的消息(msg.isSupportForward)才可转发
     * 无论是否成功回调都会返回创建出来的消息，如果不可转发则返回null
     * @param message 需要转发的消息对象
     * @param conv 目标会话
     * @param options 消息转发时的控制选项，仅对本次发送生效，默认则用null
     * @param callback
     */
    public static void forwardMessage(Message message, Conversation conv, MessageSendingOptions options, RequestCallback<Message> callback)
    {
        JMessageClient.forwardMessage(message, conv, options, callback);
    }

    /**
     * 根据对方用户名获取聊天记录的List
     *
     * @param username
     * @return 返回值是消息列表
     * 对于每一条Message常用方法：getFromUser，getContent.toJson，getCreateTime
     */
    public static List<Message> getMessageList(String username) {
        Conversation conversation = MsgUtils.getSingleConversation(username);
        return conversation.getAllMessage();
    }

    /**
     * 获得会话列表，用于信息界面展示
     * 默认降序
     *
     * @return
     */
    public static List<Conversation> getConversationList() {
        return JMessageClient.getConversationList();
    }

    /**
     * 获得会话列表，用于信息界面展示
     * 不排序
     *
     * @return
     */
    public static List<Conversation> getConversationListByDefault() {
        return JMessageClient.getConversationListByDefault();
    }

    /**
     * 根据收到的消息，返回对应的消息内容实体
     * @param msg
     * @return
     */
    public static MessageContent getMsgContent(Message msg)
    {
        if(msg.getContentType() == ContentType.text)
        {
            return (TextContent)(msg.getContent());
        }else if(msg.getContentType() == ContentType.image)
        {
            return (ImageContent)(msg.getContent());
        }else if(msg.getContentType() == ContentType.voice)
        {
            return (VoiceContent)(msg.getContent());
        }else if(msg.getContentType() == ContentType.location)
        {
            return (LocationContent)(msg.getContent());
        }else if(msg.getContentType() == ContentType.video)
        {
            return (VideoContent)(msg.getContent());
        }else if(msg.getContentType() == ContentType.eventNotification)
        {
            return (EventNotificationContent)(msg.getContent());
        }else if(msg.getContentType() == ContentType.custom)
        {
            return (CustomContent)(msg.getContent());
        }else if(msg.getContentType() == ContentType.unknown)   //对未知消息的处理方式还不确定
        {
//            PromptContent msgContent = (PromptContent)(msg.getContent());
//            msgContent.getPromptType().val
            return (PromptContent)(msg.getContent());
        }else if(msg.getContentType() == ContentType.file)
        {
            return (FileContent)(msg.getContent());
        }else if(msg.getContentType() == ContentType.prompt)
        {
            return (PromptContent)(msg.getContent());
        }else{
            return (PromptContent)(msg.getContent());
        }
    }
}
